package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysUserService;
import com.xiaoxu.intranetweb.util.MD5Util;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户密码修改业务层
 * @author xiaoxu
 * @date 2023/1/13 15:00
 * @version 1.0
 * @since 1.0
 */
@Component
public class ChangePasswordBu {
    @Autowired
    private ISysUserService iSysUserService;
    public SzResult changePassword(String token,String oldPassword, String newPassword) {
        SzResult szResult = new SzResult();
        String userId = StpUtil.getLoginIdByToken(token).toString();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", userId);
        queryWrapper.eq("PWD_DETAIL", oldPassword);
//        getOne()方法返回的是一个对象
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        if (sysUser !=null) {
            SysUser sysUser1 = new SysUser();
            sysUser1.setPwdDetail(newPassword);
            sysUser1.setPassword(MD5Util.getMd5(newPassword).toUpperCase());
            iSysUserService.update(sysUser1, queryWrapper);
            szResult.setCode(200);
            szResult.setMsg("success");
            szResult.setResult("修改成功");
            return szResult;
        } else {
            szResult.setCode(500);
            szResult.setMsg("error");
            szResult.setResult("修改失败");
            return szResult;
        }
    }
}
